<template>
    <div class="article">
        <div class="container">
             <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="小说标题" prop="bookname">
                <el-input v-model="ruleForm.bookname"></el-input>
            </el-form-item>
             <el-form-item label="封面图片" prop="imgurl">
                <el-input v-model="ruleForm.imgurl"></el-input>
            </el-form-item>
             <el-form-item label="简介" prop="introduction">
                <el-input  type="textarea" :rows="8" v-model="ruleForm.introduction"></el-input>
            </el-form-item>
              <el-form-item label="作者" prop="author">
                <el-input v-model="ruleForm.author" disabled></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
        </div>
    </div>
</template>

<script>
  export default {
    data() {
      return {
        ruleForm: {
          authorid:'',
          bookname: '',
          imgurl:'',
          introduction:'',
          author:''
        },
        rules: {
          bookname: [
            { required: true, message: '请输入小说书名', trigger: 'blur' },
            { min: 1, max: 20, message: '长度在 1到 20 个字符', trigger: 'blur' }
          ],
          imgurl: [
            { required: true, message: '请填写封面图片链接', trigger: 'blur' },
            { min: 5, max: 200, message: '长度在 5到 200 个字符', trigger: 'blur' }
          ],
           autintroductionhor: [
            { required: true,message: '请填写作品简介', trigger: 'blur' },
            { min: 2, max: 500, message: '长度在 2 到 500 个字符', trigger: 'blur' }
          ],
          author: [
            { message: '默认当前用户', trigger: 'blur' },
            { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
          ],
        }
      };
    },

    created(){
      let user=JSON.parse(sessionStorage.getItem("user"))
      this.ruleForm.author=user.author
      this.ruleForm.authorid=user.id
    },

       methods: {

      submitForm(formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {     
            let data=await this.axios.post("article/newbook",this.ruleForm)
            if(data.data.status==200){
              // this.resetForm('ruleForm')
               this.$message.success("提交成功，等待审核")
                this.$router.push("/personalCenter")
               return 
              
            }
              return  this.$message.error("提交失败")
          } else {
            // console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
 </script>
<style lang="less" scoped>
    .article {
      width:100%;
      min-height: 100vh;
      position: relative;
      background-image: linear-gradient(120deg, #84fab0 0%, #8fd3f4 100%);
  .container {
    position: absolute;
    top:50%;
    left:50%;
    transform: translate(-50%,-50%);
    .el-form{
      width:1000px;
      height: 500px;
      padding: 20px 30px;
      border-radius: 10px;
      background-image: linear-gradient(135deg, #fdfcfb 0%, #e2d1c3 100%);
      .el-form-item {
        .el-input {

        }
      }
    }
  }
}
</style>